﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Akabana.Amf.Amf3.IO;
using Akabana.Amf.Amf3.Serializer;
using Akabana.Amf.IO;
using Akabana.Amf.Serializer;

namespace Akabana.Amf.Messaging.Messages
{
    public class AcknowledgeMessageExt : AcknowledgeMessage, IExternalizable
    {

        public void writeExternal(AmfWriter writer)
        {
            using (var o = new MemoryStream())
            {
                var w = new AmfWriter(o, writer.version);
                w.amfSerializer = new Amf3Serializer(w);

                if (body == null)
                {
                    w.WriteByte(0xA8);
                    w.WriteByte(0x3);
                    w.WriteObject(headers);
                    w.WriteObject(timestamp);
                    w.WriteObject(clientId.toUIDBytes());
                    w.WriteObject(messageId.toUIDBytes());
                    w.WriteByte(0x2);
                    w.WriteObject(correlationId.toUIDBytes());
                    w.WriteByte(0x00);

                    writer.Write(o.ToArray());
                }
                else
                {
                    writer.WriteByte(0xA1);
                    writer.WriteByte(0x3);
                    w.WriteObject(body);
                    w.WriteObject(timestamp);
                    w.WriteObject(clientId.toUIDBytes());
                    w.WriteObject(messageId.toUIDBytes());
                    w.WriteByte(0x2);
                    w.WriteObject(correlationId.toUIDBytes());
                    w.WriteByte(0x00);

                    writer.Write(o.ToArray());
                }
                o.Close();
            }
        }

        public void readExternal(AmfReader reader)
        {
        }
    }
}
